<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\SurprisedGoods;
use think\Db;
/**
 *  惊喜商品管理
 *
 *  Author:  YSX
    Date:    2022-6-6
 */
class Surprise extends Api
{
    protected $noNeedLogin = ['goodsList','goodsDetail'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
    /*
     * 列表
    */
    public function goodsList()
    {
        // $params = $this->params;
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		$where['is_del'] = array('eq',1);
		$where['is_enable'] = array('eq',1);
		
		$limit    = !empty($params['limit']) ? $params['limit'] : 10;
		$page     = !empty($params['page']) ? $params['page'] : 1;
		
        $data['list'] = SurprisedGoods::where($where)
			  ->order('sort desc ,id desc')
			  ->field('id,goods_name,image,sales,price,add_time')
			  ->page($page)
			  ->limit($limit)
			  ->select(); 
		foreach($data['list'] as $key => $value){
			$data['list'][$key]['attr'] = Db::name('surprised_goods_attr')->where('surprised_goods_id',$value['id'])->column('attr_name');
		}
		$data['count']    = SurprisedGoods::where($where)->count();
		if ($data) {
			$this->success('获取成功', $data);
		}
		$this->error('请求失败');
    }

    /*
     * 详情
     */
    public function goodsDetail()
    {
        if (!$this->request->isPost()) {
        	return $this->error('提交方式不正确');
        }
		$params = $this->request->post();
		if(!isset($params['goods_id']) || empty($params['goods_id'])){
			return $this->error('参数错误');
		}
		$where['is_del'] = array('eq',1);
		$where['is_enable'] = array('eq',1);
		$data = SurprisedGoods::where('id', $params['goods_id'])
			->where($where)
			->field('id,goods_name,image,carousel_image,sales,price,introduction,add_time')
			->find();
		$data['carousel_image'] = \explode(',',$data['carousel_image']);
		$data['attr'] = Db::name('surprised_goods_attr')->where('surprised_goods_id',$params['goods_id'])->field('id,attr_name')->select();
		$this->success('获取成功', $data);
    }
	/*
	 * 加入购物车
	 */
	public function addCart()
	{
	    if (!$this->request->isPost()) {
	    	return $this->error('提交方式不正确');
	    }
		$params = $this->request->post();
		$member_id = $this->auth->id;
		$where['member_id'] = array('eq',$member_id);
		$where['surprised_goods_id'] = array('eq',$params['surprised_goods_id']);
		$where['surprised_goods_attr_id'] = array('eq',$params['surprised_goods_attr_id']);
		//判断这个商品属性是否存在
		$isFind = Db::name('surprised_cart')->where($where)->find();
		if($isFind){
			//则数量添加
			$result = Db::name('surprised_cart')->where($where)->setInc('number',$params['number']);
		}else{
			//则添加
			$data['member_id'] = $member_id;
			$data['surprised_goods_id'] = $params['surprised_goods_id'];
			$data['surprised_goods_attr_id'] = $params['surprised_goods_attr_id'];
			$data['number'] = $params['number'];
			$result = Db::name('surprised_cart')->insert($data);
		}
		if ($result) {
			$this->success('加入购物车成功');
		}
		$this->error('加入购物车失败');
	}
}
